package com.chanzany.iterator.case1;

public class CommunicationCollegeIterator implements Iterator<Department> {
    //链表的方式组织元素
    private static class Node {
        Department item;
        Node next;
        Node prev;
        Node(Node prev, Node next, Department item) {
            this.prev = prev;
            this.next = next;
            this.item = item;
        }
    }
    private Node first;
    private Node last;
    private Node cur;
    public CommunicationCollegeIterator(Department[] departments) {
        first = new Node(null,null,departments[0]);
        Node prev = first;
        last = first;
        for (int i = 1; i < departments.length; i++) {
            Node tmp = new Node(prev,null,departments[i]);
            prev.next = tmp;
            prev = tmp;
            if (i==departments.length-1) last = tmp;
        }
        cur = first;
    }

    @Override
    public boolean hasNext() {
        return cur.next!=null;
    }

    @Override
    public Department next() {
        return cur.next.item;
    }
}
